package com.letv.whatslive.model.convert;

import com.google.common.collect.Lists;
import com.letv.whatslive.model.WeidianItem;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wangjian7 on 2016/9/20.
 */
public class WeidianItemConvert {


    public static DBObject castWeidianItemToDBObject(WeidianItem weidianItem) {
        DBObject dbo = new BasicDBObject();
        if (weidianItem.getId() != null) {
            dbo.put("_id", weidianItem.getId());
        }
        dbo.put("itemid", ObjUtils.toString(weidianItem.getItemid()));
        dbo.put("istop", ObjUtils.toInteger(weidianItem.getIstop(), 0));
        dbo.put("item_desc", ObjUtils.ifNull(weidianItem.getItem_desc(), ""));
        dbo.put("item_name", ObjUtils.ifNull(weidianItem.getItem_name(), ""));
        dbo.put("fx_fee_rate", ObjUtils.ifNull(weidianItem.getFx_fee_rate(), ""));
        dbo.put("seller_id", ObjUtils.ifNull(weidianItem.getSeller_id(), ""));
        if (weidianItem.getImgs() != null && weidianItem.getImgs().size() > 0) {
            dbo.put("imgs", weidianItem.getImgs());
        } else {
            dbo.put("imgs", new ArrayList<String>());
        }
        if (weidianItem.getThumb_imgs() != null && weidianItem.getThumb_imgs().size() > 0) {
            dbo.put("thumb_imgs", weidianItem.getThumb_imgs());
        } else {
            dbo.put("thumb_imgs", new ArrayList<String>());
        }
        dbo.put("merchant_code", ObjUtils.ifNull(weidianItem.getMerchant_code(), ""));
        if (weidianItem.getSkus() != null) {
            dbo.put("skus", weidianItem.getSkus());
        }
        dbo.put("status", ObjUtils.toInteger(weidianItem.getStatus()));
        dbo.put("stock", ObjUtils.toInteger(weidianItem.getStock(), 0));
        dbo.put("sold", ObjUtils.toInteger(weidianItem.getSold(), 0));
        dbo.put("price", ObjUtils.toDouble(weidianItem.getPrice(), 999999.99));
        if (weidianItem.getCates() != null) {
            dbo.put("cates", weidianItem.getCates());
        }
        dbo.put("online", ObjUtils.toInteger(weidianItem.getOnline(), 0));
        if(weidianItem.getCreateTime()!=null){
            dbo.put("createTime", ObjUtils.toLong(weidianItem.getCreateTime()));
        }
        if(weidianItem.getUpdateTime()!=null){
            dbo.put("updateTime", ObjUtils.toLong(weidianItem.getUpdateTime()));
        }

        return dbo;
    }

    public static WeidianItem castDBObjectToWeidianItem(DBObject dbObject) {
        WeidianItem weidianItem = new WeidianItem();
        if (dbObject != null) {
            weidianItem.setId(ObjUtils.toLong(dbObject.get("_id")));
            weidianItem.setItemid(ObjUtils.toString(dbObject.get("itemid")));
            weidianItem.setIstop(ObjUtils.toInteger(dbObject.get("istop")));
            weidianItem.setItemid(ObjUtils.toString(dbObject.get("itemid")));
            weidianItem.setItem_name(ObjUtils.toString(dbObject.get("item_name")));
            weidianItem.setFx_fee_rate(ObjUtils.toString(dbObject.get("fx_fee_rate")));
            weidianItem.setSeller_id(ObjUtils.toString(dbObject.get("seller_id")));
            if (dbObject.get("imgs") != null) {
                BasicDBList dbList = (BasicDBList) (dbObject.get("imgs"));
                if (dbList.size() > 0) {
                    List<String> imgs = Lists.newArrayList();
                    for (int i = 0; i < dbList.size(); i++) {
                        imgs.add(ObjUtils.toString(dbList.get(i)));
                    }
                    weidianItem.setImgs(imgs);
                }
            }
            if (dbObject.get("thumb_imgs") != null) {
                BasicDBList dbList = (BasicDBList) (dbObject.get("thumb_imgs"));
                if (dbList.size() > 0) {
                    List<String> thumb_imgs = Lists.newArrayList();
                    for (int i = 0; i < dbList.size(); i++) {
                        thumb_imgs.add(ObjUtils.toString(dbList.get(i)));
                    }
                    weidianItem.setThumb_imgs(thumb_imgs);
                }
            }
            weidianItem.setMerchant_code(ObjUtils.toString(dbObject.get("merchant_code")));

            if (dbObject.get("skus") != null) {
                if (dbObject.get("skus") instanceof BasicDBList) {
                    BasicDBList dbList = (BasicDBList) (dbObject.get("skus"));
                    if (dbList.size() > 0) {
                        List<Map<String, Object>> skus = Lists.newArrayList();
                        for (int i = 0; i < dbList.size(); i++) {
                            if (dbList.get(i) instanceof Map) {
                                skus.add((Map<String, Object>) (dbList.get(i)));
                            } else {
                                throw new ClassCastException("skus can not cast to Map!");
                            }
                        }
                        weidianItem.setSkus(skus);
                    }
                } else if (dbObject.get("skus") instanceof BasicDBObject && ((BasicDBObject) dbObject.get("skus")).size() > 0) {
                    List<Map<String, Object>> skus = Lists.newArrayList();
                    Map<String, Object> sku = ((BasicDBObject) (dbObject.get("skus"))).toMap();
                    skus.add(sku);
                    weidianItem.setSkus(skus);
                }
            }

            weidianItem.setStatus(ObjUtils.toInteger(dbObject.get("status")));
            weidianItem.setStock(ObjUtils.toInteger(dbObject.get("stock")));
            weidianItem.setPrice(ObjUtils.toDouble(dbObject.get("price")));
            weidianItem.setSold(ObjUtils.toInteger(dbObject.get("sold")));

            if (dbObject.get("cates") != null) {
                if (dbObject.get("cates") instanceof BasicDBList) {
                    BasicDBList dbList = (BasicDBList) (dbObject.get("cates"));
                    if (dbList.size() > 0) {
                        List<Map<String, Object>> cates = Lists.newArrayList();
                        for (int i = 0; i < dbList.size(); i++) {
                            if (dbList.get(i) instanceof Map) {
                                cates.add((Map<String, Object>) (dbList.get(i)));
                            } else {
                                throw new ClassCastException("cates can not cast to Map!");
                            }
                        }
                        weidianItem.setCates(cates);
                    }
                } else if (dbObject.get("cates") instanceof BasicDBObject && ((BasicDBObject) dbObject.get("cates")).size() > 0) {
                    List<Map<String, Object>> cates = Lists.newArrayList();
                    Map<String, Object> cate = ((BasicDBObject) (dbObject.get("cates"))).toMap();
                    cates.add(cate);
                    weidianItem.setSkus(cates);
                }
            }
            weidianItem.setOnline(ObjUtils.toInteger(dbObject.get("online")));
            weidianItem.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            weidianItem.setUpdateTime(ObjUtils.toLong(dbObject.get("updateTime")));
        }
        return weidianItem;
    }
}
